<?php
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//确认出席
	case "attend":

    	$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$eventid = intval($_POST['eventid']);
		$memberid = intval($_POST['memberid']);
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));

		if($userid<>$strEvent['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在报名名单中',1);
		}


		//标记他出席了
		$new['event']->update('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$memberid,
		),array(
			'status'=>1,
		));

		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$memberid);

		/* 对服务时长进行处理
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));
		$strType = $new['event']->find('event_type',array(
			'typeid'=>$strEvent['typeid'],
		));
		if($strType['isduty'] == 1){
			$term = intval((strtotime($strEvent['endtime'])-strtotime($strEvent['starttime']))/(1000*3600));
			$arrUser=$new['user']->find('user_info',array('userid'=>$memberid));
			$count_duty=$arrUser['count_duty']*1+$term*1;
			$new['user']->update('user_info',array(
				'userid'=>$memberid,
			),array(
				'count_duty'=>$count_duty,
			));
			aac('user') -> doScore('user', 'duty', 'verify',$memberid);
		}else{


		}
*/

		//统计出席的
		$userDoNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'status'=>1,
		));

		$new['event']->update('event',array(
			'eventid'=>$eventid,
		),array(
			'count_userdo'=>$userDoNum
		));

		//event
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));

		/*
		if($userid != $memberid){
			$msg_userid = '0';
			$msg_touserid = $memberid;
			$msg_content = $TS_USER['username'].'确认你参加了活动《'.$strEvent['title'].'》，快去看看吧^_^ ';
			$msg_tourl = tsUrl('event','show',array('id'=>$eventid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}*/


		echo '1';

		break;

	//确认缺席
	case "absent":

		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$eventid = intval($_POST['eventid']);
		$memberid = intval($_POST['memberid']);
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));

		if($userid<>$strEvent['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在报名名单中',1);
		}


		//标记他出席了
		$new['event']->update('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$memberid,
		),array(
			'status'=>0,
		));

		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$memberid);

		/* 对服务时长进行处理
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));
		$strType = $new['event']->find('event_type',array(
			'typeid'=>$strEvent['typeid'],
		));
		if($strType['isduty'] == 1){
			$term = intval((strtotime($strEvent['endtime'])-strtotime($strEvent['starttime']))/(1000*3600));
			$arrUser=$new['user']->find('user_info',array('userid'=>$memberid));
			$count_duty=$arrUser['count_duty']*1+$term*1;
			$new['user']->update('user_info',array(
				'userid'=>$memberid,
			),array(
				'count_duty'=>$count_duty,
			));
			aac('user') -> doScore('user', 'duty', 'verify',$memberid);
		}else{


		}
*/

		//统计出席的
		$userDoNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'status'=>1,
		));

		$new['event']->update('event',array(
			'eventid'=>$eventid,
		),array(
			'count_userdo'=>$userDoNum
		));
		//event
		$strEvent = $new['event']->find('event',array(
			'eventid'=>$eventid,
		));

		echo '1';

		break;

	//取消参加活动
	case "cancel":

		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			echo 0;exit;
		}
		
		$eventid = intval($_POST['eventid']);
		
		$new['event']->delete('event_users',array(
		
			'eventid'=>$eventid,
			'userid'=>$userid,
		
		));
		
		//统计一下参加的
		$userWishNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'status'=>0,
		));
		


		$new['event']->update('event',array(
			'eventid'=>$eventid,
		),array(
			'count_userwish'=>$userWishNum,
		));
		
		echo '1';
		
		break;
		
	//参加活动
	case "join":
	
		$js = intval($_GET['js']);
		
	
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			getJson('请登录后再参加活动',$js);
		}
		
		$eventid = intval($_POST['eventid']);

        $strEvent = $new['event']->find('event',array(
            'eventid'=>$eventid,
        ));

        if($strEvent==''){
            getJson('不存在该活动！',$js);
        }

		if($strEvent['limittime']<=time()){
			getJson('已过报名期限',$js);
		}

		if($strEvent['duty']>0){
			$strUser = aac('user')->getOneUser($userid);
			if($strUser['count_duty']<$strEvent['duty']){
				getJson('您的服务时长没有达到报名活动的要求',$js);
			}
		}

		if($strEvent['quota']>0&&$strEvent['quota']<=$strEvent['count_userwish']){
			getJson('活动已满员',$js);
		}

        if($strEvent['price']>0){

            //是否需要支付
            if ($TS_APP['ispay'] == 1) {

                //启动支付帐号
                $strUserPay = aac('pay')->getUserPay($userid);
                if($strUserPay['over']<$strEvent['price']){
                    getJson('支付帐号资金不足，请充值后再参加活动！',$js);
                }

                //付款消费
                aac('pay')->updatePay($userid,$strEvent['price'],1,'参加收费活动'.$strEvent['eventid']);

            }

        }
		
		$username = trim($_POST['username']);
		$phone = trim($_POST['phone']);
		$orgid = trim($_POST['orgid']);
		$groupid = trim($_POST['groupid']);
		$memo = tsClean($_POST['memo']);
		aac('system')->antiWord($memo);
		
		if($username=='' || $phone==''){
			getJson('姓名和手机号不能为空',$js);
		}
		
		//删除之前的报名信息
		$new['event']->delete('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$userid,
		));
		
		
		$new['event']->create('event_users',array(
			'eventid'=>$eventid,
			'userid'=>$userid,
			'username'=>$username,
			'phone'=>$phone,
			'orgid'=>$orgid,
			'groupid'=>$groupid,
			'status'=>0,
			'addtime'=>time(),
		));

		//更新用户信息
		$new['event']-> update('user_info',array(
			'userid'=>$userid,
		),array(
			'username'=>$username,
			'phone'=>$phone,
			'groupid'=>$groupid,
			'orgid'=>$orgid
		));



		//统计报名人数
		$userWishNum = $new['event']->findCount('event_users',array(
			'eventid'=>$eventid,
			'status'=>0,
		));
		
		$new['event']->update('event',array(
			'eventid'=>$eventid,
		),array(
			'count_userwish'=>$userWishNum
		));

		if($memo){
			$new['event']->create('event_comment',array(
				'eventid'			=> $eventid,
				'userid'			=> $userid,
				'content'	=> $memo,
				'addtime'		=> time(),
			));
		}

		if($TS_USER['wxopenid']){
			$arr = array(
				'title'=>$strEvent['title'],
				'address'=>$strEvent['address'],
				'begintime'=>$strEvent['starttime']
			);
			aac('wechat')->sendwechat(5,$userid,$arr,tsUrl('event','show',array('id'=>$eventid)));
		}

		if($strEvent['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strEvent['userid'];
			$msg_content = '你的活动《'.$strEvent['title'].'》新增一个报名，快去看看吧^_^ ';
			$msg_tourl = tsUrl('event','show',array('id'=>$eventid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}


		//发一条便笺
		$content = '报名了活动《<a href="'.tsUrl('event','show',array('id'=>$eventid)).'">'.$strEvent['title'].'》！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));


		header('location: '.$_SERVER['HTTP_REFERER']);



		break;

	// 上传封面
    case "photo":
		$photo_id = mt_rand(100000, 999999);
		$dir = "event";
        $arrUpload = tsUpload($_FILES['file'], $photo_id, $dir, array('jpg','gif','png', 'jpeg'));

        if($arrUpload){
			// 删除缓存缩略图	
            tsDimg($arrUpload['url'], $dir,'48','48',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'16','16',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'32','32',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'24','24',$arrUpload['path']);
            tsDimg($arrUpload['url'], $div,'120','120',$arrUpload['path']);

			echo $arrUpload["url"];
        }else{
			echo 0;
        }

        break;
		
}
